package cc.mrbird.unit.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.grid.domain.GridInfo;
import cc.mrbird.unit.domain.UnitType;
import cc.mrbird.unit.service.UnitTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(description = "单位类型管理接口")
@RestController
public class UnitTypeController extends BaseController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UnitTypeService unitTypeService;
    @Log("获取单位类型信息")
    @RequestMapping(value = "unittype",method = RequestMethod.POST)
    /* @RequiresPermissions("unittype:list")*/
    public String index() {
        return "unit/unittype/unittype";
    }
    @Log("获取单位类型信息")
    @ApiOperation(value = "获取单位类型信息" ,  notes="获取单位类型信息")
    @RequestMapping(value = "unittype/list",method = RequestMethod.POST)
    /*@RequiresPermissions("unittype:list")*/
    @ResponseBody
    public Map<String, Object> unitTypeList(QueryRequest request, UnitType unitType) {
        return super.selectByPageNumSize(request, () -> this.unitTypeService.findAllList(unitType, request));
    }
    @ApiOperation(value = "单位类型添加")
    @RequestMapping(value = "unittype/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo add(UnitType unitType) {
        try {
            UnitType result = this.unitTypeService.findByName(unitType.getUnit_type_name());
            if (result != null) {
                return ResponseBo.warn("该单位类型已存在！");
            }
            this.unitTypeService.add(unitType);
            return ResponseBo.ok();
        } catch (Exception e) {
            log.error("添加失败", e);
            return ResponseBo.error("添加失败，请重新添加！");
        }
    }
    @ApiOperation(value = "修改单位类型信息")
    @Log("修改单位类型信息")
    /* @RequiresPermissions("unittype:update")*/
    @RequestMapping(value = "unittype/update" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo update(UnitType unitType) {
        try {
            this.unitTypeService.update(unitType);
            return ResponseBo.ok("修改单位类型信息成功！");
        } catch (Exception e) {
            log.error("修改单位类型信息失败", e);
            return ResponseBo.error("修改单位类型信息失败，请联系网站管理员！");
        }
    }
    @Log("删除单位类型信息")
    /*@RequiresPermissions("unittype:delete")*/
    @RequestMapping(value = "unittype/delete",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo delete(String ids) {
        try {
            this.unitTypeService.delete(ids);
            return ResponseBo.ok("删除单位类型信息成功！");
        } catch (Exception e) {
            log.error("删除单位类型信息失败", e);
            return ResponseBo.error("删除单位类型信息失败，请联系网站管理员！");
        }
    }
}
